package yf.mall.component.app;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.yf.response.R;
import com.yf.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import yf.mall.entity.AppCategoryEntity;
import yf.mall.entity.AppMaterialEntity;
import yf.mall.entity.AppMaterialEntityExample;
import yf.mall.entity.AppProductMaterialEntity;
import yf.mall.enums.AppMaterialFileTypeEnum;
import yf.mall.enums.app.AppCategoryTypeEnum;
import yf.mall.service.app.AppCategoryService;
import yf.mall.service.app.AppMaterialService;
import yf.mall.service.app.AppProductMaterialService;
import yf.mall.vo.app.common.ListResult;
import yf.mall.vo.app.material.*;
import yf.mall.vo.app.request.AppMaterialListRequest;
import yf.mall.vo.app.product.KolProductVo;
import yf.mall.vo.app.request.ProductListRequest;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 素材vo 处理器
 *
 * @author yxmds
 */
@Slf4j
@Component
public class AppMaterialComponent {


    @Resource
    private AppMaterialService appMaterialService;

    @Resource
    private AppProductMaterialService appProductMaterialService;

    @Resource
    private AppProductComponent appProductComponent;

    @Resource
    private QiniuFileComponent qiniuFileComponent;

    @Resource
    private AppCategoryService appCategoryService;

    private Integer Elastic_Title_TYPE = 1;

    private Integer LINE_TYPE = 1;

    private Integer Video_Title_TYPE = 1;

    public AppProductMaterialVo getProductMaterialDetail(Long pid, Long userId) {
        KolProductVo kolProductDetail = appProductComponent.getKolProductDetail(pid, userId);
        if (kolProductDetail == null) {
            return null;
        }

        AppProductMaterialVo appProductMaterialVo = convertProductMaterialVo(kolProductDetail);

        fillMaterialInfo(appProductMaterialVo, pid);

        return appProductMaterialVo;
    }

    public ListResult<AppProductMaterialVo> getProductMaterialList(ProductListRequest request) {
//        if (request.getType() != null) {
//            List<AppCategoryEntity> appCategoryEntities = appCategoryService.getCategoryByParent(request.getType().longValue(), 0L);
//            String types = appCategoryEntities.stream().map(category -> category.getType().toString()).collect(Collectors.joining(","));
//            request.setLv1(types);
//        }
        request.setHasMaterial(true);
        ListResult<KolProductVo> kolProductVoListResult = appProductComponent.getKolProductList(request, null);
        List<KolProductVo> kolProductVoList = kolProductVoListResult.getData();
        if (kolProductVoList.isEmpty()) {
            return ListResult.emptyResult;
        }
        List<AppProductMaterialVo> productMaterialVos = kolProductVoList.stream().map(product -> {
            AppProductMaterialVo productVo = convertProductMaterialVo(product);
            return productVo;
        }).collect(Collectors.toList());

        fillMaterialCountInfo(productMaterialVos, kolProductVoList);

        return new ListResult<>(productMaterialVos, kolProductVoListResult.getHasMore());
    }


    public ListResult<AppVideoMaterialVo> getVideoMaterialList(AppMaterialListRequest request) {
        AppMaterialEntityExample query = buildMaterialQuery(request);
        query.getOredCriteria().get(0).andFileTypeEqualTo(AppMaterialFileTypeEnum.VEDIO.getIndex());
        PageInfo<AppMaterialEntity> page = appMaterialService.page(query, new Page<>(request.getPage(), request.getLimit()));
        List<AppMaterialEntity> materials = page.getList();
        if (materials.isEmpty()) {
            return new ListResult<>(Collections.emptyList(), false);
        }
        List<AppVideoMaterialVo> materialVos = materials.stream().map(material -> {
            AppVideoMaterialVo materialVo = convertVideoMaterialVo(material);
            return materialVo;
        }).collect(Collectors.toList());

        return new ListResult<>(materialVos, page.isHasNextPage());
    }

    private AppMaterialEntityExample buildMaterialQuery(AppMaterialListRequest request) {
        AppMaterialEntityExample example = new AppMaterialEntityExample();
        AppMaterialEntityExample.Criteria criteria = example.createCriteria();
        if (request.getType() != null) {
            criteria.andTypeEqualTo(request.getType());
        }
        if (request.getId() != null) {
            List<AppProductMaterialEntity> dxProductMaterials = appProductMaterialService.getByProduct(request.getId());
            if (dxProductMaterials.isEmpty()) {
                criteria.andIdEqualTo(999L);
            } else {
                List<Long> materialIds = dxProductMaterials.stream().map(AppProductMaterialEntity::getMaterialId).collect(Collectors.toList());
                criteria.andIdIn(materialIds);
            }
        }
        return example;
    }

    private void fillMaterialCountInfo(List<AppProductMaterialVo> productMaterialVos, List<KolProductVo> kolProductVoList) {

    }

    private void fillMaterialInfo(AppProductMaterialVo appProductMaterialVo, Long productId) {
        List<AppProductMaterialEntity> dxProductMaterials = appProductMaterialService.getByProduct(productId);
        List<Long> materialIds = dxProductMaterials.stream().map(AppProductMaterialEntity::getMaterialId).collect(Collectors.toList());
        appProductMaterialVo.setVideoShotCount(0); //FIXME 补充素材片段数数据
        fillGraphicVo(appProductMaterialVo, materialIds);
    }

    private void fillGraphicVo(AppProductMaterialVo appProductMaterialVo, List<Long> materialIds) {
        List<AppMaterialEntity> AppMaterialEntitys = appMaterialService.listByMaterialIds(materialIds, AppMaterialFileTypeEnum.TEXT.getIndex());
        AppGraphicVo appGraphicVo = convertGraphicVo(AppMaterialEntitys);
        appProductMaterialVo.setGraphic(appGraphicVo);
    }

    public void fillProductVo(KolProductVo productVo, Long productId) {
        List<AppProductMaterialEntity> dxProductMaterials = appProductMaterialService.getByProduct(productId);
        List<Long> materialIds = dxProductMaterials.stream().map(AppProductMaterialEntity::getMaterialId).collect(Collectors.toList());
        List<AppMaterialEntity> AppMaterialEntityList = appMaterialService.listByIds(materialIds);
        String promotion = AppMaterialEntityList.stream().filter(m -> Objects.equals(m.getType(), AppCategoryTypeEnum.PROMOTION.getType())).map(AppMaterialEntity::getContent).findFirst().orElse("");
        String videoUrl = AppMaterialEntityList.stream().filter(m -> Objects.equals(m.getType(), AppCategoryTypeEnum.VIDEO_URL.getType())).map(AppMaterialEntity::getUrl).findFirst().orElse("");
        productVo.setPromotion(promotion);
        productVo.setVideoUrl(qiniuFileComponent.complementFileUrl(videoUrl));
    }


    private AppProductMaterialVo convertProductMaterialVo(KolProductVo kolProductDetail) {
        AppProductMaterialVo appProductMaterialVo = new AppProductMaterialVo();
        appProductMaterialVo.setCover(kolProductDetail.getCover());
        appProductMaterialVo.setId(kolProductDetail.getPid());
        appProductMaterialVo.setProductInfo(kolProductDetail);
        appProductMaterialVo.setPublishTime(kolProductDetail.getAddTime());
        appProductMaterialVo.setDownloaded(false);
        appProductMaterialVo.setShowSubscribeBtn(false);
        appProductMaterialVo.setSubscribed(false);
        appProductMaterialVo.setRemainderDownlTime(0);
        return appProductMaterialVo;
    }

    private AppGraphicVo convertGraphicVo(List<AppMaterialEntity> AppMaterialEntitys) {
        AppGraphicVo appGraphicVo = new AppGraphicVo();
        appGraphicVo.setElasticTitles(AppMaterialEntitys.stream().filter(m -> Objects.equals(m.getType(), AppCategoryTypeEnum.ELASTRTC_TITLE_TYPE.getType())).map(m -> convertWrittenMaterialVo(m)).collect(Collectors.toList()));
        appGraphicVo.setLines(AppMaterialEntitys.stream().filter(m -> Objects.equals(m.getType(), AppCategoryTypeEnum.LINE_TYPE.getType())).map(m -> convertWrittenMaterialVo(m)).collect(Collectors.toList()));
        appGraphicVo.setVideoTitles(AppMaterialEntitys.stream().filter(m -> Objects.equals(m.getType(), AppCategoryTypeEnum.VIDEO_Title_TYPE.getType())).map(m -> convertWrittenMaterialVo(m)).collect(Collectors.toList()));

        return appGraphicVo;
    }

    private AppVideoMaterialVo convertVideoMaterialVo(AppMaterialEntity AppMaterialEntity) {
        AppVideoMaterialVo appVideoMaterialVo = new AppVideoMaterialVo();
        appVideoMaterialVo.setCover(AppMaterialEntity.getCover());
        appVideoMaterialVo.setId(AppMaterialEntity.getId().toString());
        appVideoMaterialVo.setDownloaded(false);
        appVideoMaterialVo.setPublishTime(DateUtil.dateToStr(AppMaterialEntity.getPublishTime()));
        appVideoMaterialVo.setSize(AppMaterialEntity.getSize());
        appVideoMaterialVo.setTimes(AppMaterialEntity.getTimes());
        appVideoMaterialVo.setTitle(AppMaterialEntity.getTitle());
        appVideoMaterialVo.setUrl(qiniuFileComponent.complementFileUrl(AppMaterialEntity.getUrl()));
        appVideoMaterialVo.setType(AppMaterialEntity.getType());
        appVideoMaterialVo.setTypeName("");  // 获取分类名称
        return appVideoMaterialVo;
    }

    private AppWrittenMaterialVo convertWrittenMaterialVo(AppMaterialEntity AppMaterialEntity) {
        AppWrittenMaterialVo appWrittenMaterialVo = new AppWrittenMaterialVo();
        appWrittenMaterialVo.setContent(AppMaterialEntity.getContent());
        return appWrittenMaterialVo;
    }

}
